/**
 * problem_020.c
 * Copyright (C) 2011-03-19 - xrose
 */

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
char *mul_ui(char *op1, unsigned int op2);
int sum(char* param);
int main (int argc, char *argv[])
{
    char *op1 ="1";
//    unsigned int op2 = 100;
    char *result;
//    result = mul_ui(op1, op2);
    int i;
    for(i=100; i>0; i--)
    {
        result = mul_ui(op1, i);
        op1 = result;
    }
    printf("__%s\n", result);
    printf("___+++%d\n", sum(result));
    free(result);
    return 0;
}
int sum(char* param)
{
    printf("+++sum()%s\n", param);
    int _sum = 0;
    int i = 0;
    while(param[i]!='\0')
    {
        _sum +=(param[i]-'0');
        i++;
    }
    return _sum;
}
char *mul_ui(char *op1, unsigned int op2)
{
    int _lgn = floor(log10(op2)) + 1; //number of digit of op2
    int i = 0;
    int j = 0;
    int tmp =0;
    int r =0;
    int l_op =0;
    char *buffer;
    char *b_op;
    while(op1[i] != '\0')
    {
        _lgn++;
        i++;
    }
    l_op = i;
    buffer = (char*)malloc(_lgn+1);
    if (buffer==NULL) exit (1);
    for(i=0; i<_lgn; i++)
    {
        buffer[i] = '0';
    }
    buffer[_lgn] = '\0';

    b_op = (char*)malloc(_lgn+1);
    if(b_op == NULL) exit(1);
    for(i=0; i<_lgn; i++)
    {
        b_op[i] = '0';
    }
    b_op[_lgn] = '\0';
    for(i=0; i<=l_op; i++)
    {
        b_op[_lgn - i] = op1[l_op - i];
    }
//    printf("_buffer: %s\n", buffer);
    for(i=1; i<=op2; i++)
    {
        for(j=_lgn-1; j>=0; j--)
        {
            tmp = (b_op[j]-'0') + (buffer[j]-'0') + r;
            if(tmp < 10)
            {
                buffer[j] = tmp+'0';
                r = 0;
            } else
            {
                buffer[j] = tmp - 10 + '0';
                r = 1;
            }
        }
    }
    return buffer;
}
